﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.IO;
using System.Data.SqlClient;
using System.Configuration;
using System.Data;



namespace DATN.mds.ContactManagement
{
    public partial class ExportExcel : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                if (Session["Us_Id"] != null)
                {
                    //Do something here!
                    BindData();
                    //Note Remind
                    Session["Remind"] = null;
                }
                else
                {
                    Response.Redirect("../Account/Login.aspx");
                }
                Validate();
            }
               
        }
        //Bind Data
        protected void BindData()
        {
            try
            {
                SqlConnection cnn = new SqlConnection();
                cnn.ConnectionString = ConfigurationManager.ConnectionStrings["DATNconnection"].ToString();
                cnn.Open();

                string queryS = "Select * From Customers Where Us_Id =" + Convert.ToInt16(Session["Us_Id"].ToString());
                SqlDataAdapter da = new SqlDataAdapter(queryS, cnn);
                DataSet ds = new DataSet();
                da.Fill(ds, "Customers");

                Grv_EEx.DataSource = ds.Tables["Customers"];
                Grv_EEx.DataBind();
                ds.Dispose();
                da.Dispose();
                cnn.Close();
            }
            catch (Exception ex)
            {
                Response.Write("Lỗi : " + ex);
            }
        }

          int noNumber = 0;
        //Access RowDataBound
          protected void Grd_ContactAll_RowDataBound(object sender, GridViewRowEventArgs e)
          {
              if (e.Row.RowType == DataControlRowType.DataRow)
              {
                  DataRowView row = e.Row.DataItem as DataRowView;

                  // Hiển thị cho column Giới Tính
                  Label Lb_Sex = e.Row.FindControl("Lb_Sex") as Label;
                  if (Lb_Sex != null)
                  {
                      if (row["Sex"].ToString() == "1")
                      {
                          Lb_Sex.Text = "Nam";
                      }
                      if (row["Sex"].ToString() == "2")
                      {
                          Lb_Sex.Text = "Nữ";
                      }
                      if (row["Sex"].ToString() == "3")
                      {
                          Lb_Sex.Text = "Khác";
                      }
                  }

                  if (e.Row.DataItemIndex == -1)
                      return;

                  e.Row.Attributes.Add("onMouseOver", "this.style.cursor='hand';");
                  if ((e.Row.RowIndex % 2) == 0)
                  {
                      e.Row.Attributes.Add("onmouseover", "this.style.backgroundColor='#aafdaa'");
                      e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor='#b4cbd5'");
                  }
                  else
                  {
                      e.Row.Attributes.Add("onmouseover", "this.style.backgroundColor='#aafdaa'");
                      e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor='#FFFFFF'");
                  }
              }
          }

        //Export to Doc file
        protected void btnExportToDoc_Click(object sender, EventArgs e)
        {
            try
            {
                if (tbx_FileName.Text == "")
                {
                    Response.AddHeader("content-disposition", "attachment;filename= midas_contact.doc");
                }
                else
                {
                    Response.AddHeader("content-disposition", "attachment;filename=" + tbx_FileName.Text + ".doc");
                    Response.Cache.SetCacheability(HttpCacheability.NoCache);
                    Response.ContentType = "application/vnd.word";
                    System.IO.StringWriter stringWrite = new System.IO.StringWriter();
                    System.Web.UI.HtmlTextWriter htmlWrite = new HtmlTextWriter(stringWrite);
                    // Create a form to contain the grid
                    HtmlForm frm = new HtmlForm();
                    Grv_EEx.Parent.Controls.Add(frm);
                    frm.Attributes["runat"] = "server";
                    frm.Controls.Add(Grv_EEx);
                    frm.RenderControl(htmlWrite);
                    //GridView1.RenderControl(htw);
                    Response.Write(stringWrite.ToString());
                    Response.End();
                }
            }
            catch (Exception ex)
            {
                Lb_Stt.Text = "Lỗi ở export doc";
            }
        }

        //Export to Excel file
        protected void btnExportToExcel_Click(object sender, EventArgs e)
        {
            try
            {
                Response.ClearContent();
                if (tbx_FileName.Text == "")
                {
                    Response.AddHeader("content-disposition", "attachment;filename= midas_contact.xls");
                }
                else
                {
                    Response.AddHeader("content-disposition", "attachment;filename=" + tbx_FileName.Text + ".xls");
                    Response.ContentType = "application/ms-excel";
                    StringWriter sw = new StringWriter();
                    HtmlTextWriter htw = new HtmlTextWriter(sw);
                    // Create a form to contain the grid
                    HtmlForm frm = new HtmlForm();
                    Grv_EEx.Parent.Controls.Add(frm);
                    frm.Attributes["runat"] = "server";
                    frm.Controls.Add(Grv_EEx);
                    frm.RenderControl(htw);

                    //GridView1.RenderControl(htw);
                    Response.Write(sw.ToString());
                    Response.End();
                }
            }
            catch (Exception ex)
            {
                Lb_Stt.Text = "Lỗi ở export Excel!";
            }
        }
     

      
    }
}